package logic

import (
	"context"
	"go-zero2/rpc/greet2/greet2"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"

	"go-zero2/rpc/greet/greet"
	"go-zero2/rpc/greet/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PingLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLogic {
	return &PingLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PingLogic) Ping(in *greet.Request) (*greet.Response, error) {
	// todo: add your logic here and delete this line

	conn, err := grpc.Dial("greet2:8081", grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Println(err)
		return nil, err
	}
	client := greet2.NewGreet2Client(conn)
	resp, err := client.Ping(context.Background(), &greet2.Request{})
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return &greet.Response{Pong: "greet_v2 -->>  " + resp.Pong}, nil

}
